package ie.hurley.dao.impl;
import java.util.List;

import ie.hurley.dao.LoginDAO;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import ie.hurley.model.*;


public class LoginDAOImpl extends HibernateDaoSupport implements LoginDAO{

	public String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	@Override
	public String login(String username, String password,String type) {
		// TODO Auto-generated method stub
		if(type.equals("student")){
			String hql = new String("from students as u where u.stu_id = " + username );
			@SuppressWarnings("unchecked")
			List<students> list = (List<students>)this.getHibernateTemplate().find(hql);
			for(students stu:list){
				if(stu.getPassword().equals(password)){
					message = stu.getStu_name();
					return "student";
				}
			}
		}
		else if(type.equals("teacher")){
			String hql = new String("from teachers as u where u.teacher_id = " + username );
			@SuppressWarnings("unchecked")
			List<teachers> list = (List<teachers>)this.getHibernateTemplate().find(hql);
			for(teachers tea:list){
				if(tea.getPassword().equals(password)){
					message = tea.getTeacher_name();
					return "teacher";
				}
			}
		}
		message = "not exist this user!";
		return message;
	}

}
